package net.wenku8.reader.api

import com.alibaba.fastjson2.JSON
import net.wenku8.reader.model.book.BookCaseItem
import net.wenku8.reader.model.book.BookDataList
import net.wenku8.reader.model.book.BookInfo
import net.wenku8.reader.model.book.BookPreviewInfo
import net.wenku8.reader.model.book.VolumeList
import net.wenku8.reader.model.book.VolumeList2
import net.wenku8.reader.model.rank.RankList
import net.wenku8.reader.model.rank.SearchResult
import net.wenku8.reader.model.user.SignDataList
import net.wenku8.reader.model.user.UserDataList
import net.wenku8.reader.model.user.UserInfo
import net.wenku8.reader.model.user.UserSignInfo
import net.wenku8.reader.util.lowerFirst
import org.json.JSONObject

fun itemAsUserInfo(userDataList: UserDataList?): UserInfo {
    val json = JSONObject()
    userDataList?.list?.forEach { e ->
        json.put(e.name!!, e.value!!)
    }
    return JSON.parseObject(json.toString(), UserInfo::class.java)
}

fun itemAsUserSignInfo(userDataList: SignDataList?): UserSignInfo {
    val json = JSONObject()
    userDataList?.list?.forEach { e ->
        json.put(e.name!!, e.value!!)
    }
    return JSON.parseObject(json.toString(), UserSignInfo::class.java)
}

fun dataAsBook(bookDataList: BookDataList?) : BookInfo {
    val json = JSONObject()
    bookDataList?.list?.forEach { e ->
        json.put(e.name!!.lowerFirst(), e.value)
        if (e.bookId != null){
            json.put("bookId", e.bookId)
            json.put(e.name!!.lowerFirst(), e.text)
        }
        if (e.chapterId != null){
            json.put("chapterId", e.chapterId)
            json.put(e.name!!.lowerFirst(), e.text)
        }
    }
    return JSON.parseObject(json.toString(), BookInfo::class.java)
}


fun searchAsPreview(searchResult: SearchResult?) : ArrayList<BookPreviewInfo> {
    val list = ArrayList<BookPreviewInfo>()
    searchResult?.list?.forEach { e ->
        val json = JSONObject()
        json.put("bookId", e.bookId)
        e.list.forEach { v->
            if (v.value != null){
                json.put(v.name!!.lowerFirst(), v.value)
            }else{
                json.put(v.name!!.lowerFirst(), v.text)
            }
        }
        val t = JSON.parseObject(json.toString(), BookPreviewInfo::class.java)
        list.add(t)
    }
    return list
}

fun rankAsPreview(rankList: RankList?) : ArrayList<BookPreviewInfo> {
    val list = ArrayList<BookPreviewInfo>()
    rankList?.list?.forEach { e ->
        val json = JSONObject()
        json.put("bookId", e.bookId)
        e.list.forEach { v->
            if (v.value != null){
                json.put(v.name!!.lowerFirst(), v.value)
            }else{
                json.put(v.name!!.lowerFirst(), v.text)
            }
        }
        val t = JSON.parseObject(json.toString(), BookPreviewInfo::class.java)
        list.add(t)
    }
    return list
}


fun bookCaseAsPreview(bookList : ArrayList<BookCaseItem>?) : ArrayList<BookPreviewInfo> {
    val list = ArrayList<BookPreviewInfo>()
    bookList?.forEach { e ->
        val preview = BookPreviewInfo()
        preview.bookId = e.bookId
        preview.title = e.name
        preview.lastUpdate = e.date
        preview.lastChapter = e.chapter.title
        preview.inBookcase = true
        list.add(preview)
    }
    return list
}

fun mergeBookTitle(bookIndex: VolumeList?, volumeList2: VolumeList2?){
    if (bookIndex == null || volumeList2 == null) return
    bookIndex.volumeList.withIndex().forEach{it ->
        val volume2 = volumeList2.volumeList[it.index]
        it.value.title = volume2.title.trim()
    }
}